@use './HistoryList.module.scss';

$chat-narrow-container: 600px;
$chat-very-narrow-container: 400px;

.messageItem {
    --chat-hidden-message-max-height: 7.5em;

    display: flex;
    padding: 1em;
    gap: 2px;
    margin-bottom: 20px;
    position: relative;
    align-items: flex-start;
    margin: 5px 0;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
    overflow-wrap: break-word;
    word-wrap: break-word;
    word-break: break-word;

    // Add focus styles
    &:focus {
        outline: none;
    }

    &:focus-visible {
        outline: none;

        .messageContainer {
            .message {
                box-shadow: 0px 0px 3px 2px var(--b3-theme-primary);
            }
        }
    }

    @container (max-width: #{$chat-narrow-container}) {
        padding: 0.8em;
        gap: 0.8em;
    }

    @container (max-width: #{$chat-very-narrow-container}) {
        padding: 0.6em;
        gap: 0.6em;
    }

    .versionIndicator {
        --horizontal-padding: 1em;
        --vertical-padding: 1em;

        position: absolute;
        top: var(--vertical-padding);
        right: var(--horizontal-padding);
        z-index: 2;
        cursor: pointer;
        padding: 4px;
        border-radius: 4px;
        display: flex;
        align-items: center;
        gap: 4px;
        background-color: var(--b3-theme-surface);
        border: 1px solid var(--b3-border-color);
        font-size: 12px;
        opacity: 0.8;
        transition: opacity 0.2s ease;

        &:hover {
            opacity: 1;
            background-color: var(--b3-theme-surface-light);
        }

        svg {
            width: 14px;
            height: 14px;
            fill: var(--b3-theme-on-surface);
        }
    }

    &[data-role="user"] {
        flex-direction: row-reverse;

        .messageContainer {
            display: flex;
            flex-direction: column;
            align-items: flex-end;

            margin-right: 0;
            margin-left: max(5%, 20px);

            @container (max-width: #{$chat-very-narrow-container}) {
                margin-left: 1px;
            }

            .toolbar {
                // flex-direction: row-reverse;
                .toolbar-text, .toolbar-buttons {
                    flex-direction: row-reverse;
                }
            }
        }

        .versionIndicator {
            right: unset;
            left: var(--horizontal-padding);
        }
    }

    .icon {
        flex-shrink: 0;
        width: 20px;
        height: 20px;
        margin: 0 5px;

        @container (max-width: #{$chat-narrow-container}) {
            width: 16px;
            height: 16px;

            position: absolute;
            top: 5px;
            z-index: 10;

            &[data-role="user"] {
                right: 5px;
            }

            &[data-role="assistant"] {
                left: 5px;
            }
        }

        @container (max-width: #{$chat-very-narrow-container}) {
            width: 12px;
            height: 12px;
        }

        svg {
            width: 100%;
            height: 100%;
            fill: var(--b3-theme-on-surface);
            opacity: 0.6;
        }
    }

    .messageContainer {
        position: relative;
        flex: 1;
        min-width: 0;
        display: flex;
        flex-direction: column;
        gap: 5px;

        margin-left: 0;
        margin-right: max(5%, 20px);

        .reasoningDetails {
            border-radius: 4px;
            border: 1px solid var(--b3-border-color);
            background-color: var(--chat-assistant-bg);

            summary {
                padding: 8px 12px;
                cursor: pointer;
                user-select: none;
                font-size: 14px;
                color: var(--b3-theme-on-surface);

                &:hover {
                    background-color: var(--b3-theme-surface);
                }
            }

            .reasoningContent {
                padding: 12px 16px;
                border-top: 1px solid var(--b3-border-color);
                background-color: var(--chat-assistant-bg);
                font-size: var(--chat-message-font-size) !important;
                line-height: 1.5;
                user-select: text;
                -webkit-user-select: text;
                -moz-user-select: text;
                -ms-user-select: text;
            }
        }

        &:not([data-role="user"]) {
            @container (max-width: #{$chat-very-narrow-container}) {
                margin-right: 1px;
            }
        }

        &:hover {
            .toolbar {
                opacity: 1;
            }
        }

        .message {
            padding: 12px 16px;
            border-radius: 8px;
            font-size: var(--chat-message-font-size) !important;
            line-height: 1.5;
            // color: var(--chat-text-color);
            overflow-x: auto;
            box-shadow: 0px 0px 3px 1px var(--b3-theme-primary-lighter);
            user-select: text;
            -webkit-user-select: text;
            -moz-user-select: text;
            -ms-user-select: text;
            max-width: 100%;

            &.user {
                background: var(--chat-user-bg);
            }

            &.assistant {
                background: var(--chat-assistant-bg);
            }

            &.hidden {
                opacity: 0.5;
                max-height: var(--chat-hidden-message-max-height);
                font-size: calc(var(--chat-message-font-size) * 0.85) !important;
            }

            :global(pre) {
                position: relative;
                margin: 8px 0;
                padding: 0;
                background-color: var(--b3-theme-surface);
                border: 1px solid var(--b3-theme-on-surface-light);
                overflow: hidden;
                max-width: 100%;

                :global(code) {
                    display: block;
                    padding: 16px;
                    overflow-x: auto;
                    font-size: 0.9em;
                    line-height: 1.45;
                    background: transparent;
                    border: none;
                    white-space: pre;
                    word-wrap: normal;
                }
            }
        }

        .toolbar {
            opacity: 0;
            transition: opacity 0.2s ease;
            display: flex;
            flex-direction: column;
            align-items: flex-start;
            gap: 7px;
            padding: 0 4px;
            font-size: 12px;
            color: var(--b3-theme-on-surface);
            flex-wrap: wrap;

            @container (max-width: #{$chat-narrow-container}) {
                >span {

                    &[data-label="timestamp"],
                    &[data-label="msgLength"],
                    &[data-label="attachedChars"],
                    &[data-label="token"] {
                        display: none;
                    }
                }
            }

            @container (max-width: #{$chat-very-narrow-container}) {

                // labels
                &>span {
                    display: none;
                }
            }

            .toolbar-text {
                display: flex;
                flex-wrap: wrap;
                gap: 7px;
                width: 100%;
            }

            .toolbar-buttons {
                display: flex;
                flex-wrap: wrap;
                gap: 7px;
                width: 100%;
            }

            button {
                padding: 2px;
                opacity: 0.6;

                &:hover {
                    opacity: 1;
                }

                svg {
                    width: 14px;
                    height: 14px;
                }
            }
        }
    }

    .checkbox {
        position: absolute;
        left: 0.1em;
        top: 0.2em;
        cursor: pointer;
        padding: 0.2em;
        border-radius: 4px;

        &:hover {
            background-color: var(--b3-theme-background-light);
        }

        svg {
            height: 16px;
            width: 16px;
            display: block;
        }
    }
}

.code-toolbar {
    display: flex;
    align-items: center;
    padding: 2px 12px;
    gap: 8px;
    border-bottom: 1px solid var(--b3-border-color);
    background-color: var(--b3-theme-surface);
    line-height: 1.0;
}

.toolbarButton {
    padding: 2px !important;
    opacity: 0.6;
    display: inline-flex;
    align-items: center;
    justify-content: center;

    &:hover {
        opacity: 1;
    }

    svg {
        width: 14px;
        height: 14px;
        margin: 0px;
    }
}

.katex-center-display {
    display: flex;
    justify-content: center;
    margin: 1em 0;
}

.streamingText {
    // font-family: var(--b3-font-family-code);
    // font-size: 90%;
    padding: 8px 0;
    margin-top: 8px;
    border-top: 1px dashed var(--b3-border-color);
    white-space: pre-wrap;
    color: var(--b3-theme-on-surface);
    opacity: 0.85;
}

.pre-scroll-container {
    overflow-x: auto;
    max-width: 100%;
}